import { BulletLayer, RoleBuff, RoleSkill } from "../../config/role_def";

cc.Class({
    extends: cc.Component,

    properties: {
        bullet_layer:cc.Node,       // 子弹层级 怪物层级上面
        effect_layer:cc.Node,       // 特效层级 怪物层级下面
        topeffect_layer:cc.Node,    // 顶部特效层级 所有层级上
        bullet_list:[],
        idCount: {
            visible:false,
            default:1,
            type:cc.Integer
        },
    },
    // 根据id 删除对应子弹
    delBullet(id){
        if(this.bullet_list.length){
            for(let i = this.bullet_list.length - 1; i >= 0; i--){
                if(this.bullet_list[i].orderId == id){
                    if(this.bullet_list[i].script_name){ // 删除子弹脚本并且入库
                        this.bullet_list[i].node.removeComponent(this.bullet_list[i].script_name)
                        if(this.bullet_list[i].pool){
                            he.pool.putElement(this.bullet_list[i].pool,this.bullet_list[i].node)
                        }else{
                            he.pool.putElement('bullet',this.bullet_list[i].node)
                        }
                    }
                    this.bullet_list.splice(i,1)
                    return
                }
            }
        }
    },
    // 添加子弹 默认为怪物上层
    addBullet(param){
        let bullet = he.pool.getElement('bullet')
        
        if(param.angle){
            bullet.angle = -param.angle
        }
        let script = bullet.addComponent(param.script)

        script.orderId = this.idCount
        this.idCount += 1
        if(this.idCount >= cc.macro.MAX_ZINDEX){
            this.idCount = 1
        }
        if(param.layer){
            switch(param.layer){
                case BulletLayer.BULLET_LAYER:
                    bullet.position = this.bullet_layer.convertToNodeSpaceAR(param.pos)
                    this.bullet_layer.addChild(bullet)
                    break
                case BulletLayer.EFFECT_LAYER:
                    bullet.position = this.effect_layer.convertToNodeSpaceAR(param.pos)
                    this.effect_layer.addChild(bullet)
                    break
                case BulletLayer.TOP_LAYER:
                    bullet.position = this.topeffect_layer.convertToNodeSpaceAR(param.pos)
                    this.topeffect_layer.addChild(bullet)
                    break
            }
        }else{
            bullet.position = this.bullet_layer.convertToNodeSpaceAR(param.pos)
            this.bullet_layer.addChild(bullet)
        }
        script.initInfo(param)
        this.bullet_list.push(script)
        return script
    },
    // 怪物子弹 默认为最高层
    addDeBullet(param){
        let bullet = he.pool.getElement('debullet')
        let script = bullet.addComponent(param.script)
        script.orderId = this.idCount
        this.idCount += 1
        if(this.idCount >= cc.macro.MAX_ZINDEX){
            this.idCount = 1
        }
        if(param.layer){
            switch(param.layer){
                case BulletLayer.BULLET_LAYER:
                    bullet.position = this.bullet_layer.convertToNodeSpaceAR(param.pos)
                    this.bullet_layer.addChild(bullet)
                    break
                case BulletLayer.EFFECT_LAYER:
                    bullet.position = this.effect_layer.convertToNodeSpaceAR(param.pos)
                    this.effect_layer.addChild(bullet)
                    break
                case BulletLayer.TOP_LAYER:
                    bullet.position = this.topeffect_layer.convertToNodeSpaceAR(param.pos)
                    this.topeffect_layer.addChild(bullet)
                    break
            }
        }else{
            bullet.position = this.bullet_layer.convertToNodeSpaceAR(param.pos)
            this.bullet_layer.addChild(bullet)
        }
        script.initInfo(param)
        this.bullet_list.push(script)
        return script
    },
    // 添加特效
    addEffect(param){
        if(!he.pool.getPool(param.pool)){
            he.pool.createPoolSync(param.pool,5,cc.Prefab,param.prefab)
            gg.uiManager.pool_list.push({ name : param.pool, prefab: param.prefab })
        }
        let effect = he.pool.getElement(param.pool)
        let scriptName = ''
        if(param.script){
            scriptName = param.script
        }else{
            scriptName = param.pool
        }
        let script = effect.getComponent(scriptName)
        if(param.scale){
            effect.scale = param.scale
        }
        effect.position = this.effect_layer.convertToNodeSpaceAR(param.pos)
        script.orderId = this.idCount
        this.idCount += 1
        if(this.idCount >= cc.macro.MAX_ZINDEX){
            this.idCount = 1
        }
        this.effect_layer.addChild(effect)
        script.initInfo(param)
        this.bullet_list.push(script)
    },
    // 添加顶级特效
    addTopEffect(param){
        if(!he.pool.getPool(param.pool)){
            he.pool.createPoolSync(param.pool,5,cc.Prefab,param.prefab)
        }
        let effect = he.pool.getElement(param.pool)
        let scriptName = ''
        if(param.script){
            scriptName = param.script
        }else{
            scriptName = param.pool
        }
        let script = effect.getComponent(scriptName)
        if(param.scale){
            effect.scale = param.scale
        }
        effect.position = this.topeffect_layer.convertToNodeSpaceAR(param.pos)

        script.orderId = this.idCount
        this.idCount += 1
        if(this.idCount >= cc.macro.MAX_ZINDEX){
            this.idCount = 1
        }
        this.topeffect_layer.addChild(effect)
        script.initInfo(param)
        this.bullet_list.push(script)
    },
    addBulletEffect(bullet,extra){
        if(extra){
            let param = extra.split("#")
            switch(parseInt(param[0])){
                case RoleBuff.Crit:
                    bullet.effect.push({
                        key: RoleSkill.Crit, 
                        uid: RoleSkill.Crit,
                        pro: parseInt(param[1]),            // 概率
                        mul: parseInt(param[2])             // 倍数
                    })
                    break
                case RoleBuff.SlowBullet:
                    bullet.effect.push({
                        key: RoleSkill.BoidSLow, 
                        uid: RoleSkill.BoidSLow,
                        value: parseInt(param[1]),                // 减速值
                        time: parseInt(param[2]),                // 持续时间
                    })
                    break
                case RoleBuff.RepelBullet:
                    bullet.effect.push({
                        key: RoleSkill.Repel,
                        value: parseInt(param[1]),                // 参数值 伤害值
                        repel: parseInt(param[2]),                // 多长时间发动一次
                    })
                break
            }
        }
        return bullet
    },
    // 清除全屏子弹
    clearBullet(){
        for(let i = this.bullet_list.length - 1;i >= 0;i--){
            this.bullet_list[i].clearInfo()
        }
    }
});
